package com.example.win.android_banduyujia.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by Win on 2015/9/28.
 */
public class CustomListDialog extends LinearLayout {

    private Activity  mActivity;
    private String    mTitle;
    private List<String>  mContentLis;

    private TextView  mTitleTextVew;
    private ListView  mContentListView;

    private View view;
    private PopupWindow mPopupWindow;

    public CustomListDialog(Activity activity, int titleId, int contentItems) {
        this(activity, titleId, Arrays.asList(activity.getResources().getStringArray(contentItems)));
    }

    public CustomListDialog(Activity activity, int titleId, String[] contentItems) {
        this(activity, titleId, Arrays.asList(contentItems));
    }

    public CustomListDialog(Activity activity, int titleId, List<String> contentItemList) {
        this(activity, activity.getResources().getString(titleId), contentItemList);
    }

    public CustomListDialog(Activity activity, String title, List<String> contentList) {
        //super(context);
        super(activity.getBaseContext());
        mActivity = activity;
        mTitle = title;
        mContentLis = contentList;

        LayoutInflater.from(activity.getBaseContext()).inflate(R.layout.custom_list_dialog, this);

        setViewValues();
        initDialogPopupWindow();
    }

    public void show(){
     //  mPopupWindow.showAtLocation(mActivity.getCurrentFocus(), Gravity.BOTTOM, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public void dismiss(){
        mPopupWindow.dismiss();
    }

    public void setOnItemClickListener(OnItemClickListener  listener){
        mContentListView.setOnItemClickListener(listener);
    }

    private void setViewValues() {
        mTitleTextVew = (TextView)findViewById(R.id.custom_dialog_list_title);
        mTitleTextVew.setText(mTitle);

        mContentListView=(ListView)findViewById(R.id.custom_dialog_list_view);

        CustomListAdapter mAdapter = new CustomListAdapter(mActivity.getBaseContext(), mContentLis);
        mContentListView.setAdapter(mAdapter);
    }

    private void initDialogPopupWindow() {
        mPopupWindow = new PopupWindow(this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
       // mPopupWindow.showAtLocation(mActivity.getCurrentFocus(), Gravity.BOTTOM, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        ColorDrawable  dw = new ColorDrawable(Color.TRANSPARENT);
        mPopupWindow.setBackgroundDrawable(dw);
    }
}
